define(["app","js/init",'js/util/toast','js/risk/riskModel','js/patient/listController'], 
	function(app, base,toast,model,lister) {
	
	
	var $$ = Dom7;
	
	var curConfig = {
		typeCode	: 'heavy',
		typeName	: '危重患者',
		data		: null
	}
	var bindings = [];	

	function init(query){		
	}

	/**
	 * 获取危重患者数据
	 * @param _options.method	方法名称
	 * @param _options.param	参数
	 * @param _options.callback	回调函数
	 */
	function getPie(){
		
		
		
		if(!app.mainView){
			app = app.getApp();
		}		
		
		
		var _cv = app.f7.getCurrentView();

		if(_cv && _cv.router){				
			
			url = base.base + base.appBase +'/'+(model.getCC()).typeCode+'/getCriticalDept.m';	
			$.ajax({
				url	: url,
				data: {userid:user.userid,bizcode:curConfig.typeCode},
				ansyc: false,
				success: function(data){
					curConfig.data = data;
					loadPie(_cv.router,data);
				}
				,
				error:function(){
					app.f7.hidePreloader();
					toast.show({msg:'数据加载失败！'})
				}
			})
			
			
		}else{
			app.f7.views.risk.router.loadPage('app/patient/list.html')
		}
		app.f7.showPreloader('loading...');
	}
	
	function pieClick(e){
		
		
		if(!app.mainView){
			app = app.getApp();
		}

		
		
		var type = e.point.code; 
				
		
			var _cv = app.f7.getCurrentView();			
			var _router
			if(_cv){
				_router = _cv.router			
			}else{
				_router = app.f7.views.risk.router			
			}
			
			if(curConfig.data){
				
				var _data = []
				var _f =type=='normal'?'cnt':'problem_cnt';
				$.each(curConfig.data,function(i,o){
					if(o[_f] && o[_f]>0){
						o.title = '危重'
						o.subtitle = '在院'
						o.detail = '危重比:'+((o.problem_cnt /o.cnt) *100).toFixed(1) + '%';
						_data.push(o);
					}
				})
				lister.renderDept(_data);
			}
			
			//_router.reloadPage('app/risk/'+typeCode + '.html');
		
	}
		
	function loadPie(router,data){
		
		if(!app.mainView){
			app = app.getApp();
		}	
		var c_all=0,c_n=0;c_h=0
		if(data){
			
			$.each(data,function(i,o){
				c_all +=o.cnt;
				c_h +=o.problem_cnt;
			})
			c_n = c_all-c_h;
		}
		
		var c = model.getCC();
		router.load({			
			url:'app/patient/list.html',
			query:{
				module: c.module,
				typeCode: c.typeCode,
				bizcode: c.bizcode,
				bizName: curConfig.typeName,
				title		: '危重患者比例',
				subtitle	: '在院患者合计: ' + c_all + ' 人/床, 危重患者合计:' + c_h + ' 人/床',
				onClick : pieClick,
				imgSrc : 'img/png/'+c.module+'/'+c.typeCode+'/'+curConfig.typeCode+'-grey.png',
				data		: [
	        		{name:"危重患者",code:'problem',y:c_h},
	        		{name:"非危重患者",code:'normal',y:c_n}
	            ],
	            legendLabel	: '{name}: <b>{y}</b>人次 , 占比 <b>{percentage:.1f}%</b>'
			}
		});
		
		app.f7.hidePreloader();
	}
	
	
	return {
		init	: init,
		getPie	: getPie
	};
});